package com.blog.service.impl;

import java.security.Principal;
import java.util.Collection;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;


@Service("userHolder")
public class UserHolder{
	UserDetails userDetails;
	
	public UserDetails getUserDetails(){
		
		if(SecurityContextHolder.getContext().getAuthentication().getPrincipal()!="anonymousUser"){
			userDetails= (UserDetails) SecurityContextHolder.getContext()
				    .getAuthentication()
				    .getPrincipal();
			//str=userDetails.getUsername()+userDetails.getPassword()+userDetails.isAccountNonExpired();
		}else{
			userDetails=null;
		}
		return userDetails;
	}
	
	public Collection<? extends GrantedAuthority> getPreMission(){
		userDetails=getUserDetails();
		if(userDetails!=null){
			return userDetails.getAuthorities();
		}
		return null;
	}
	
	public String getUserName() {
		userDetails=getUserDetails();
		if(userDetails!=null){
			return userDetails.getUsername();
		}
		return "anonymous";
	   }


}
